﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.NoticeUI
{
    public partial class PublishedNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetAllNotices();
            }
        }

        private void GetAllNotices()
        {
            NoticeManager manager = new NoticeManager();
            noticesGdv.DataSource = manager.GetAllNotices();
            noticesGdv.DataBind();
        }

        protected void Published(object o, EventArgs e)
        {
            try
            {
                Notice notice=new Notice();
                notice.Date = txtDate.Value;
                notice.Subject = txtSubject.Value;
                notice.Summary = txtSummary.Value.Replace("13","</br>");

                string noticeImage = Path. GetFileName(UploadImage.PostedFile.FileName);
                UploadImage.PostedFile.SaveAs(Server.MapPath("~/NoticeImage/") + noticeImage);
                notice.NoticeImage = "~/NoticeImage/" + noticeImage+ "-" + notice.Date;
                notice.NoticeBy = txtAuthoruty.Value;

                NoticeManager manager=new NoticeManager();
                if(manager.PublishNotice(notice))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Notice Published Successfully";
                    ClearInputs();
                    GetAllNotices();
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Notice Not Published"; 
                }

            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text =ex.Message; 
            }
        }

        private void ClearInputs()
        {
            txtDate.Value = "";
            txtSubject.Value = "";
            txtSummary.Value = "";
            txtAuthoruty.Value = "";
        }
    }
}